<template>
	<view>
		<view class="page__bd">
			<view class="weui-grids1">
				<view>
					<view class="weui-grid1" hover-class="weui-grid_active" v-for="(item,index) in fllist" :key="index" @tap="goCustomers(index)">
						<image class="weui-grid__icon" :src="item.image" mode="widthFix" />
						<view class="weui-grid__label ">{{item.name}}</view>
					</view>
				</view>
			</view>
		</view>
		<!--选择批次modal-->
		<view class="cu-modal" :class="showMolde?'show':'hide'">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="cuIcon-home content">{{isStare?"盘点批次":"结束盘点"}}</view>
				</view>
				<view class="">
					<view class="cu-form-group">
						<view class="title">盘点仓库：</view>
						<input class="inpt text-left" placeholder="<单据选择仓库>" placeholder-style="color:blue" name="input" @tap="chooseStock"
						 :value="StoreName" disabled="true"></input>
					</view>
					<view class="cu-form-group">
						<view class="title">盘点批次：</view>
						<input class="inpt text-left" :value="Inventorybatch" disabled="true"></input>
					</view>
					<view class="cu-form-group">
						<view class="title">制单人：</view>
						<input class="inpt text-left" :value="OperatorName" disabled="true"></input>
					</view>
					<view class="cu-form-group">
						<view class="title">创建时间：</view>
						<input class="inpt text-left" :value="CreateTime" disabled="true"></input>
					</view>
					<view class="cu-form-group" v-show="isStare == false">
						<view class="title">已盘点单数：</view>
						<input class="inpt text-left" :value="PDCount" disabled="true"></input>
						<!-- <label class="inpt text-left flex" disabled="true">{{PDCount}}张</label> -->
					</view>
				</view>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub text-red solid-right" @tap="hideModal(0)">取消</view>
					<view class="action margin-0  flex-sub  text-blue solid-left" v-show="isStare == true" @tap="hideModal(1)">删除盘点</view>
					<view class="action margin-0  flex-sub  text-blue solid-left" v-show="isStare == true" @tap="hideModal(2)">创建盘点</view>
					<view class="action margin-0  flex-sub  text-blue solid-left" v-show="isStare == false" @tap="hideModal(3)">结束盘点</view>
				</view>
			</view>
		</view>
		<!--选择仓库modal-->
		<view class="cu-modal" :class="showSmodle?'show':'hide'">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="cuIcon-home content">选择仓库</view>
					<view class="action" @tap="ShideModal">
						<text class="cuIcon-close text-blue"></text>
					</view>
				</view>
				<radio-group class="block" @change.stop.prevent="StockChange" @tap="hideModals(0)">
					<view class="cu-list menu text-left">
						<view class="cu-item" v-for="(item,index) in storeList" :key="index">
							<label class="flex justify-between align-center flex-sub">
								<view class="title1">{{item.StoreName}}</view>
								<radio class="round" :class="radio=='radio'?'checked':''" :checked="radio=='radio'?true:false" :value="item.StoreID"></radio>
								<!-- <jiuaiCheckbox @change='changeBox' :value="item.value" :checked="item.checked"></jiuaiCheckbox> -->
							</label>
						</view>
					</view>
				</radio-group>
			</view>
		</view>
		<!--选择结束盘点仓库modal-->
		<view class="cu-modal" :class="showEmodle?'show':'hide'">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="cuIcon-home content">选择仓库</view>
					<view class="action" @tap="ShideModal">
						<text class="cuIcon-close text-blue"></text>
					</view>
				</view>
				<radio-group class="block" @change.stop.prevent="StockChange" @tap="hideModals(0)">
					<view class="cu-list menu text-left">
						<view class="cu-item" v-for="(item,index) in storeList" :key="index">
							<label class="flex justify-between align-center flex-sub">
								<view class="title1">{{item.StoreName}}</view>
								<radio class="round" :class="radio=='radio'?'checked':''" :checked="radio=='radio'?true:false" :value="item.StoreID"></radio>
								<!-- <jiuaiCheckbox @change='changeBox' :value="item.value" :checked="item.checked"></jiuaiCheckbox> -->
							</label>
						</view>
					</view>
				</radio-group>
			</view>
		</view>
	</view>
</template>

<script>
	import * as Api from '@/syApis/syAddOrder/syaddApis.js';
	export default {
		data() {
			return {
				isStare: true,
				radio: '',
				focuspw: true,
				showMolde: false,
				showSmodle: false,
				showEmodle: false,
				storeList: [],
				Inventorybatch: '',
				StoreName: '',
				StoreID: "",
				OperatorName: uni.getStorageSync("OperatorName"),
				CreateTime: '',
				PDCount: '',
				SelectIndex: 0,
				fllist: [{
						image: "/static/kehu.png",
						name: "开始盘点",
						// url: "../customers/customers"
					},
					{
						image: "/static/kehu.png",
						name: "盘点单",
						// url: " ../add-order-Inventory/add-order-Inventory?BillName=盘点单"
					},
					{
						image: "/static/kehu.png",
						name: "结束盘点",
						// url: "../customers/customers"
					}
				]

			}
		},

		// onLoad(options) {
		// 	if (options.type == 0) {
		// 		this.isStare = true;
		// 	} else {
		// 		this.isStare = false;
		// 	}
		// },
		created() {
			let date = new Date();
			this.BillDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
		},
		methods: {
			goCustomers(e) {
				var $this = this;
				$this.Inventorybatch = '';
				$this.StoreName = '';
				$this.OperatorName = '';
				$this.CreateTime = '';
				$this.PDCount = '';
				$this.radio = "";
				if (e == 0) {
					$this.isStare = true;
					$this.showMolde = true
				} else if (e == 2) {
					$this.isStare = false;
					$this.showMolde = true
				} else if (e == 1) {
					uni.navigateTo({
						url: "../../pages/add-order-Inventory/add-order-Inventory?BillName=盘点单 "
					})
				}
			},
			hideModal(e) {
				//0是取消,1是删除。2是创建，3是结束盘点
				if (e == 0) {
					this.showMolde = false
				} else if (e == 1) {
					var _this = this;
					console.log(333333)
					if (this.StoreName == "") {
						Api.showTip("先选择仓库")
					}
					if (this.Inventorybatch == "") {
						Api.showTip("此仓库没有盘点批次")
					} else {
						Api.showConfirm("是否删除盘点？", function(state) {
							if (state) {
								// _this.Delete($data);
								let data = {
									Inventorybatch: _this.Inventorybatch,
									StoreName: _this.StoreName,
									OperatorName: _this.OperatorName,
									CreateTime: _this.CreateTime,
									PDCount: _this.PDCount
								}

								Api.Delete_Inventorybatch(data, function(res) {
									if (res.state) {
										if (res.data.ErrorMsg == "获取登录信息失败") {
											// api.showTip()
										} else if (res.data.ErrorMsg != "") {
											Api.showTip(res.data.ErrorMsg)
										} else {
											Api.showTip("删除成功");
											_this.Inventorybatch = '';
											_this.StoreName = '';
											_this.OperatorName = '';
											_this.CreateTime = '';
											_this.PDCount = '';
											_this.radio = "";

										}
									} else {
										Api.showTip("服务器连接超时")
									}
								})
							} else {
								// Api.showTip("取消登录");
								console.log("点击了取消")
							}
						})
					}

				} else if (e == 2) {
					var that = this;
					// Api.Add_Inventorybatch(data, function(res) {
					// let data = {}
					if (that.StoreName == '') {
						Api.showTip("先选择仓库")
					}
					if (that.Inventorybatch != '') {
						Api.showTip("此仓库已经存在盘点批次，请先删除或者结束盘点后再创建")
					}
					if (that.StoreName != '' && that.Inventorybatch == '') {
						// else {
						Api.showConfirm("是否创建盘点", function(state) {
							if (state) {
								let data = {
									CreateTime: that.BillDate,
									BillDate: that.BillDate,
									// Inventorybatch: that.Inventorybatch,
									StoreID: that.storeList[that.SelectIndex].StoreID,
									// OperatorName: that.OperatorName,
								}
								// var that = that;
								Api.Add_Inventorybatch(data, function(res) {
									if (res.state) {
										if (res.data.ErrorMsg == "获取登录信息失败") {
											// api.showTip()
										} else if (res.data.ErrorMsg != "") {
											Api.showTip(res.data.ErrorMsg)
										} else {
											Api.showTip("创建成功");
											// that.Inventorybatch = Inventorybatch;
											that.StoreName = "";
											// that.OperatorName = OperatorName;
											// .CreateTime = BillDate;
										}
									} else {
										Api.showTip("服务器连接超时")
									}

								})
								that.showMolde = false
							} else {
								console.log(111)
							}
						})
						// }

					}

					// })
				} else if (e == 3) {
					var _this = this;
					console.log(333333)
					if (this.StoreName == "") {
						Api.showTip("先选择仓库")
					} else {
						Api.showConfirm("是否结束盘点？结束盘点会汇总所有此批次的盘点单计算到库存", function(state) {
							if (state) {
								// _this.Delete($data);
								let data = {
									Inventorybatch: _this.Inventorybatch,
									StoreID: _this.storeList[_this.SelectIndex].StoreID,
									OperatorName: _this.OperatorName,
									CreateTime: _this.CreateTime,
									PDCount: _this.PDCount
								}

								Api.End_Inventorybatch(data, function(res) {
									if (res.state) {
										if (res.data.ErrorMsg == "获取登录信息失败") {
											// api.showTip()
										} else if (res.data.ErrorMsg != "") {
											Api.showTip(res.data.ErrorMsg)
										} else {
											Api.showTip("结算成功，库存已更改");
											_this.showMolde = false;
											// Inventorybatch = '';
											// StoreName = '';
											// OperatorName = '';
											// CreateTime = '';
											// PDCount = '';

										}
									} else {
										Api.showTip("服务器连接超时")
									}
								})
							} else {
								// Api.showTip("取消登录");
								console.log("点击了取消")
							}
						})
					}
				}

			},

			ShideModal(e) {
				this.showSmodle = false
			},
			chooseStock(e) {
				if (this.isStare == true) {
					let data = {}
					var that = this;
					Api.GetStoreMessage(data, function(res) {
						if (res.state) {
							if (res.data.ErrorMsg == "获取登录信息失败") {
								// api.showTip()
							} else if (res.data.ErrorMsg != "") {
								Api.showTip(res.data.ErrorMsg)
							} else {
								that.storeList = res.data.ResultMsg;
							}
						} else {
							Api.showTip("服务器连接超时")
						}
					});
					this.showSmodle = true;
				} else {
					let data = {}
					var that = this;
					this.storeList = [];
					Api.Select_Inventorybatch(data, function(res) {
						if (res.state) {
							if (res.data.ErrorMsg == "获取登录信息失败") {
								// api.showTip()
							} else if (res.data.ErrorMsg != "") {
								Api.showTip(res.data.ErrorMsg)
							} else {
								// that.storeList = res.data.ResultMsg;
								res.data.ResultMsg.forEach((item, index) => {
									if (item.StoreName != '') {
										// var items = {
										// 	StoreName: item.StoreName
										// }
										that.storeList.push(item);
									}
								})
							}
						} else {
							Api.showTip("服务器连接超时")
						}
					});
					this.showSmodle = true;
				}

			},
			hideModals(e) {
				// console.log("111");
				this.showSmodle = false;
				// this.SelectIndex = index;

				// this.storeList.forEach((item, index) => {
				// 	if (storeId == item.StoreID) {
				this.StoreName = this.storeList[this.SelectIndex].StoreName;
				this.StoreID = this.storeList[this.SelectIndex].StoreID;
				// that.SelectIndex = index;
				// 	}
				// });
				this.Inventorybatch = "";
				this.CreateTime = "";
				this.OperatorName = "";
				this.PDCount = "";
				let data = {
					Inventorybatch: this.Inventorybatch,
					StoreName: this.StoreName,
					OperatorName: this.OperatorName,
					CreateTime: this.CreateTime,
					PDCount: this.PDCount
				}
				// uni.setStorageSync('Inventorybatch',this.Inventorybatch);
				// uni.setStorageSync('StoreName',this.StoreName);
				// uni.setStorageSync('OperatorName',this.OperatorName);
				// uni.setStorageSync('CreateTime',this.CreateTime);
                var that = this;
				Api.Select_Inventorybatch(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg == "获取登录信息失败") {
							// api.showTip()
						} else if (res.data.ErrorMsg != "") {
							Api.showTip(res.data.ErrorMsg)
						} else {
							let respose = res.data.ResultMsg;
							console.log("_-----");
							//console.tablele(respose);
							respose.forEach((item, index) => {
								// console.log(item.StoreID, storeId);
								if (item.StoreID == that.StoreID) {
									// that.StoreName = item.StoreName;
									that.Inventorybatch = item.Inventorybatch;
									that.CreateTime = item.CreateTime;
									that.OperatorName = item.OperatorName;
									that.PDCount = item.PDCount;

								}
							})
						}
					} else {
						api.showTip("服务器连接超时")
					}
				});
				this.showSmodle = false;
			},
			StockChange(e) {
				// console.log(e);
				var storeId = e.detail.value;
				this.StoreID = storeId;
				var that = this;
				console.log(storeId);
				this.storeList.forEach((item, index) => {
					if (storeId == item.StoreID) {
						// that.StoreName = item.storeList[this.SelectIndex].StoreName;
						that.SelectIndex = index;
					}
				});
			}

		}
	}
</script>

<style>
	@import url("../index/index.css");
	@import url("../../style/weui.css");

	.title1 {
		min-width: 90px;
		font-size: 15px;
	}

	.zt {
		font-size: 15px;
	}

	.menu {
		min-height: 320px;
		max-height: 320px;
		overflow: auto;
	}
</style>
